<?php
/***********************************************************
	Filename: app/www/models/cart.php
	Note	: 购物车表中涉及到的操作
	Version : 3.0
	Author  : qinggan
	Update  : 2010-05-07
***********************************************************/
class cart_m extends Model
{
	var $sessid;
	function __construct()
	{
		parent::Model();
		$this->db->close_cache();
	}

	function cart_m()
	{
		$this->__construct();
	}

	function sessid($sessid)
	{
		$this->sessid = $sessid;
	}

	function get_all()
	{

		$sql = "SELECT * FROM ".$this->db->prefix."cart WHERE sessid='".$this->sessid."'";
		return $this->db->get_all($sql);
	}

	function add($title="",$price=0,$amount=1,$id=0)
	{
		if($id)
		{
			$sql = "SELECT * FROM ".$this->db->prefix."cart WHERE sessid='".$this->sessid."' AND proid='".$id."'";
			$rs = $this->db->get_one($sql);
			if($rs)
			{
				$amount += $rs["amount"];
				$sql = "UPDATE ".$this->db->prefix."cart SET title='".$title."',amount='".$amount."',price='".$price."',postdate='".time()."' WHERE id='".$rs["id"]."'";
				return $this->db->query($sql);
			}
			else
			{
				$sql = "INSERT INTO ".$this->db->prefix."cart(sessid,proid,title,amount,price,postdate) VALUES('".$this->sessid."','".$id."','".$title."','".$amount."','".$price."','".time()."')";
				return $this->db->query($sql);
			}
		}
		else
		{
			$sql = "INSERT INTO ".$this->db->prefix."cart(sessid,proid,title,amount,price,postdate) VALUES('".$this->sessid."','".$id."','".$title."','".$amount."','".$price."','".time()."')";
			return $this->db->query($sql);
		}
	}

	function update($id,$amount=0)
	{
		$sql = "UPDATE ".$this->db->prefix."cart SET amount='".$amount."',postdate='".time()."' WHERE id='".$id."' AND sessid='".$this->sessid."'";
		return $this->db->query($sql);
	}

	//删除
	function del($id=0)
	{
		$sql = "DELETE FROM ".$this->db->prefix."cart WHERE sessid='".$this->sessid."'";
		if($id)
		{
			$sql .= " AND id='".$id."' ";
		}
		return $this->db->query($sql);
	}
}
?>